/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;
/**
 * 说明：
 * 仓库调拨
 * @Author：
 * @Date：2012-03-21
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumnModel;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.commons.util.jna.QueryMember;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.JPanelGridLayoutComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.MemberQueryCardModel;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BasePagePanel;

public class MemberCardPanel extends BasePagePanel<TUser> implements Runnable, QueryMember {
	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent cardNumTF = null; //卡号
	private JLabelJTextFieldComponent nameTF = null;//姓名
	private JLabelJTextFieldComponent tel = null;//电话
	private DataPackageComponent startTimeTF = null;//发卡时间
	private DataPackageComponent timeTF = null;//时间
	
	private JButton queryButton;//查询
	private JButton clearButton;//清空
	private JButton levelButton; //设置会员级别
	private JButton giftButton; //送礼品
	private JButton pointButton; //送积分
	private JButton cashButton; //送优惠卷
	private JButton chargeButton; //充值
	private JButton noticeButton; //通知
	private JButton memberDetailButton; //查看会员行为
	private JButton showButton; //查看详细
	private JButton dGiftButton; //兑换礼品
	//private JButton addButton; //添加进货单
	private JTable table;
	private JScrollPane tableJsp;
	private MemberQueryCardModel memberQueryCardModel;
	private MenberDetailDialog menberDetailDialog;//会员详细窗体
	private MemberChargeDetailDialog memberChargeDetailDialog;//充值窗口
	private MemberPointSend memberPointSend; //赠送积分
	private MemberCashSend memberCashSend;//赠送优惠卷
	private MemberGiftSend memberGiftSend;//赠送礼品
	private MemberLevelPanel memberLevelPanel;//设置会员级别
	private MemberBehaviorDialog memberBehaviorDialog;//查看会员行为
	private MemberDGiftDialog memberDGiftDialog;//兑换礼品
	private MemberNoticeDetailDialog memberNoticeDetailDialog;//通知会员
	//private StoreTransferPanelAddDialog storeTransferPanelAddDialog;//仓库调拨添加页面
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TUser> pageView;//翻页主键
	private TUserService tUserService;//仓库调拨操作对象
	public MemberCardPanel() {
		tUserService=BuildSpringApplication.getInstance().getTUserServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
		this.termQuery(1);
	}
	private void init() {
		pageView = new PageView<TUser>();
		memberQueryCardModel = new MemberQueryCardModel();
		table=uiFactory.createRPJTable(memberQueryCardModel, null, this, 30, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		TableColumnModel columnModel = table.getColumnModel();
		columnModel.getColumn(0).setCellEditor(table.getDefaultEditor(Boolean.class));
		columnModel.getColumn(0).setCellRenderer(table.getDefaultRenderer(Boolean.class));
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		JPanel main_jp=new JPanel(new BorderLayout());
		main_jp.setBackground(Util.PANEL_COLOR);
		main_jp.add(this.initTop(), BorderLayout.NORTH);
		main_jp.add(tableJsp, BorderLayout.CENTER);
		main_jp.add(this.initTail(), BorderLayout.SOUTH);
		this.add(main_jp);
	}
	
	private JPanel initTop() {
		QueryPanel queryPanel=new QueryPanel("优惠卷管理",ImageFactory.loadImage(ImageFactory.FUN_7_ICON_1));
		JPanel top_panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		tel = new JLabelJTextFieldComponent("手机：", 15);
		cardNumTF = new JLabelJTextFieldComponent("卡号：", 15);
		nameTF = new JLabelJTextFieldComponent("姓名：", 15);
		startTimeTF = new DataPackageComponent("发卡时间：", 15);
		timeTF = new DataPackageComponent("时间：", 15);
		queryButton = new RPJButtonBlue("查询");
		clearButton= new RPJButtonGray("清空");
		top_panel.add(cardNumTF);
		top_panel.add(nameTF);
		top_panel.add(tel);
		top_panel.add(startTimeTF);
		top_panel.add(timeTF);
		top_panel.add(queryButton);
		top_panel.add(clearButton);
		queryPanel.addPanel(top_panel);
		return queryPanel;
	}
	
	private JPanel initTail() {
		//addButton = new JButton("添加调拨单(Ctrl+A)");
		showButton= new RPJButtonGray("查看详细");
		dGiftButton = new RPJButtonRoundBlue("兑换礼品");
		levelButton=new RPJButtonRoundBlue("设置会员级别");
		pointButton=new RPJButtonRoundBlue("送积分");
		giftButton=new RPJButtonRoundBlue("送礼品");
		cashButton=new RPJButtonRoundBlue("送优惠");
		chargeButton=new RPJButtonRoundBlue("充值");
		noticeButton=new RPJButtonRoundBlue("通知");
		memberDetailButton=new RPJButtonRoundBlue("查看会员行为");
		JPanel jp2 = new JPanelFlowLayoutComponent(new Component[]{showButton,dGiftButton,levelButton,giftButton,pointButton,cashButton,noticeButton,chargeButton,memberDetailButton}, FlowLayout.LEFT);
		JPanel tail_panel = new JPanelGridLayoutComponent(new Component[]{pagingComponent,jp2}, 2, 1);
		return tail_panel;
	}
	private void initListener(){
		clearButton.addActionListener(this);
		queryButton.addActionListener(this);
		//addButton.addActionListener(this);
		showButton.addActionListener(this);
		dGiftButton.addActionListener(this);
		levelButton.addActionListener(this);
		pointButton.addActionListener(this);
		giftButton.addActionListener(this);
		cashButton.addActionListener(this);
		chargeButton.addActionListener(this);
		noticeButton.addActionListener(this);
		memberDetailButton.addActionListener(this);
		//addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询
			this.termQuery(1);
		}else if(e.getSource()==showButton){
			//查看详细
			if(table.getSelectedRow()!=-1){
				String userID = (String)table.getValueAt(table.getSelectedRow(), -1);
				try {
					TUser tUser = tUserService.getByIdService(userID);
					menberDetailDialog = new MenberDetailDialog(null, "会员详细信息", true);
					menberDetailDialog.showDialog(tUser);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==dGiftButton){
			//兑换礼品
			if(table.getSelectedRow()!=-1){
				String userID = (String)table.getValueAt(table.getSelectedRow(), -1);
				try {
					TUser tUser = tUserService.getByIdService(userID);
					tUser.setUserID(userID);
					memberDGiftDialog = new MemberDGiftDialog(null, "兑换礼品", true);
					memberDGiftDialog.showDialog(tUser);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==levelButton){
			//设置会员级别
			if(table.getSelectedRow()!=-1){
					boolean[] bools = memberQueryCardModel.getBools();
					boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
					for (int i = bools.length -1 ; i >= 0; --i) {
						if (bools[i]) {
							select = true;
							break;
						}
					}
					if (!select) {
						JOptionPane.showMessageDialog(null, "请先选择用户！");
						return;
					} else {
						try {
							//TUser tUser = tUserService.getByIdService(userID);
							
							
							List<TUser> tUserList = memberQueryCardModel.getTCommodityList();
							List<TUser> tUserListOn = new ArrayList();
							for(int i=0;i<tUserList.size();i++){
								if (bools[i]) {
								tUserListOn.add(tUserList.get(i));
								}
							}
							memberLevelPanel = new MemberLevelPanel(null, "设置会员级别", true);
							memberLevelPanel.showDialog(tUserListOn);
							table.updateUI();
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==giftButton){
			//送礼品
			if(table.getSelectedRow()!=-1){
				boolean[] bools = memberQueryCardModel.getBools();
				boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
				for (int i = bools.length -1 ; i >= 0; --i) {
					if (bools[i]) {
						select = true;
						break;
					}
				}
				if (!select) {
					JOptionPane.showMessageDialog(null, "请先选择用户！");
					return;
				} else {
					try {
						//TUser tUser = tUserService.getByIdService(userID);
						
						List<TUser> tUserList = memberQueryCardModel.getTCommodityList();
						List<TUser> tUserListOn = new ArrayList();
						for(int i=0;i<tUserList.size();i++){
							if (bools[i]) {
							tUserListOn.add(tUserList.get(i));
							}
						}
						memberGiftSend = new MemberGiftSend(null, "会员赠送礼品", true);
						memberGiftSend.showDialog(tUserListOn);
						table.updateUI();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==pointButton){
			//送积分
			if(table.getSelectedRow()!=-1){
				boolean[] bools = memberQueryCardModel.getBools();
				boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
				for (int i = bools.length -1 ; i >= 0; --i) {
					if (bools[i]) {
						select = true;
						break;
					}
				}
				if (!select) {
					JOptionPane.showMessageDialog(null, "请先选择用户！");
					return;
				} else {
					try {
						//TUser tUser = tUserService.getByIdService(userID);
						
						List<TUser> tUserList = memberQueryCardModel.getTCommodityList();
						List<TUser> tUserListOn = new ArrayList();
						for(int i=0;i<tUserList.size();i++){
							if (bools[i]) {
							//Object obj1=tUserList.get(i);
							tUserListOn.add(tUserList.get(i));
							//TRoleRight roleRight=new TRoleRight();
							//roleRight.setTRight((TRight)obj1);
							}
						}
						memberPointSend = new MemberPointSend(null, "会员赠送积分", true);
						memberPointSend.showDialog(tUserListOn);
						table.updateUI();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==cashButton){
			//送优惠
			if(table.getSelectedRow()!=-1){
				boolean[] bools = memberQueryCardModel.getBools();
				boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
				for (int i = bools.length -1 ; i >= 0; --i) {
					if (bools[i]) {
						select = true;
						break;
					}
				}
				if (!select) {
					JOptionPane.showMessageDialog(null, "请先选择用户！");
					return;
				} else {
					try {
						//TUser tUser = tUserService.getByIdService(userID);
						
						List<TUser> tUserList = memberQueryCardModel.getTCommodityList();
						List<TUser> tUserListOn = new ArrayList();
						for(int i=0;i<tUserList.size();i++){
							if (bools[i]) {
							//Object obj1=tUserList.get(i);
							tUserListOn.add(tUserList.get(i));
							//TRoleRight roleRight=new TRoleRight();
							//roleRight.setTRight((TRight)obj1);
							}
						}
						memberCashSend = new MemberCashSend(null, "会员赠送优惠卷", true);
						memberCashSend.showDialog(tUserListOn);
						table.updateUI();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==chargeButton){
			//充值
			if(table.getSelectedRow()!=-1){
				//String userID = (String)table.getValueAt(table.getSelectedRow(), 0);
				boolean[] bools = memberQueryCardModel.getBools();
				boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
				for (int i = bools.length -1 ; i >= 0; --i) {
					if (bools[i]) {
						select = true;
						break;
					}
				}
				if (!select) {
					JOptionPane.showMessageDialog(null, "请先选择用户！");
					return;
				} else {
				try {
					//TUser tUser = tUserService.getByIdService(userID);
					
					List<TUser> tUserList = memberQueryCardModel.getTCommodityList();
					List<TUser> tUserListOn = new ArrayList();
					for(int i=0;i<tUserList.size();i++){
						if (bools[i]) {
						//Object obj1=tUserList.get(i);
						tUserListOn.add(tUserList.get(i));
						//TRoleRight roleRight=new TRoleRight();
						//roleRight.setTRight((TRight)obj1);
						}
					}
					memberChargeDetailDialog = new MemberChargeDetailDialog(null, "会员充值", true);
					memberChargeDetailDialog.showDialog(tUserListOn);
					table.updateUI();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==memberDetailButton){
			//查看会员行为
			if(table.getSelectedRow()!=-1){
				String userID = (String)table.getValueAt(table.getSelectedRow(), -1);
				try {
					TUser tUser = tUserService.getByIdService(userID);
					tUser.setUserID(userID);
					memberBehaviorDialog = new MemberBehaviorDialog(null, "会员行为", true);
					memberBehaviorDialog.showDialog(tUser);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==noticeButton){
			//通知
			if(table.getSelectedRow()!=-1){
				boolean[] bools = memberQueryCardModel.getBools();
				boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
				for (int i = bools.length -1 ; i >= 0; --i) {
					if (bools[i]) {
						select = true;
						break;
					}
				}
				if (!select) {
					JOptionPane.showMessageDialog(null, "请先选择用户！");
					return;
				} else {
				try {
					
					List<TUser> tUserList = memberQueryCardModel.getTCommodityList();
					List<TUser> tUserListOn = new ArrayList();
					for(int i=0;i<tUserList.size();i++){
						if (bools[i]) {
						tUserListOn.add(tUserList.get(i));
						}
					}
					memberNoticeDetailDialog = new MemberNoticeDetailDialog(null, "通知会员", true);
					memberNoticeDetailDialog.showDialog(tUserListOn);
					table.updateUI();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==clearButton){
			//清空
			cardNumTF.getText().setText("");
			nameTF.getText().setText("");
			tel.getText().setText("");
			startTimeTF.setDate(null);
			timeTF.setDate(null);
		}
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	@Override
	public void mouseClicked(MouseEvent e) {
    	if(e.getSource()==table&&e.getClickCount() == 2){
    		//表格
    		String id=table.getValueAt(table.getSelectedRow(), -1).toString();
    		try {
				TUser user=tUserService.getByIdService(id);
				if(user==null){
					JOptionPane.showMessageDialog(null, "当前会员不存在");
					return;
				}
				menberDetailDialog=new MenberDetailDialog(null, "会员详细", true);
	    		menberDetailDialog.showDialog(user);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
    	}
    }
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TUser user=new TUser();
		user.setCardNum(cardNumTF.getText().getText().trim());
		user.setRealName(nameTF.getText().getText().trim());
		user.setSendCardTime(startTimeTF.getDate());
		user.setMobile(tel.getText().getText());
		user.setEndTime(timeTF.getDate());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TUser> pageModel = tUserService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),user);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						memberQueryCardModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						memberQueryCardModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TUser> getPageView() {
		return pageView;
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
        ApplicationContext.listenCard(cardNumTF.getText(), ApplicationContext.CardType.CT_DLSended);
        ApplicationContext.query_member = this;
	}

    @Override
    public void queryMember() {
        queryButton.doClick();
    }
}